package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private RequestMappingHandlerMapping rmhm;
    @Autowired
    private PermissionMapper mapper;

    @Override
    public void save(Permission entity) {
        mapper.insert(entity);
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission entity) {
        mapper.updateByPrimaryKey(entity);
    }

    @Override
    public Permission get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return mapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        //查总条数
        int total = mapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        //查分页数量
        List<Department> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void load() {
        /* 去重 */
        List<Permission> permissions = mapper.selectAll();
        Set<String> set = new HashSet<>();
        for(Permission s:permissions){
            set.add(s.getResources());
        }
        //通过请求的映射处理处理器
        //通过请求的映射处理处理器对象获取该对象所有的方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        RequiresPermissions anno=null;
        Permission permission = null;
        for(HandlerMethod method :values){
            anno =  method.getMethodAnnotation(RequiresPermissions.class);
            if(anno != null){
                String resources = anno.value()[0];
                if(set.contains(resources)){
                    continue;
                }
                //获取权限名称
                String name = method.getMethodAnnotation(PermissionName.class).value();
                permission = new Permission();
                permission.setName(name);
                permission.setResources(resources);
                mapper.insert(permission);
            }
        }
    }

    @Override
    public List<Permission> queryPermissionByRoleId(Long roleId) {
        return mapper.queryPermissionByRoleId(roleId);
    }
}
